﻿Ext.define('HIT.view.ReportUserDefineGrid', {
	extend : 'Ext.grid.Panel',
	alias : 'widget.Report',

	cls : 'custom-grid',
	frame : false,
	width : 1200,
	columnLines : false,
	sortableColumns : true,
	store: 'Report',

	initComponent : function() {
		var me = this;

		function renderColor(value, cellmeta, record, rowIndex, columnIndex, store) { 
        	return "<span style='color:blue;cursor:pointer;'>" + value + "</span>";
        }  
		
		Ext.applyIf(me, {
			columns : [ {
				xtype : 'rownumberer',
				resizable: true,
				width : 30
			}, {
				xtype : 'gridcolumn',
				width : 100,
				align : 'center',
				dataIndex : 'report_hid',
				text : Share.T('报告ＩＤ')
			}, {
				xtype : 'gridcolumn',
				width : 150,
				align : 'center',
				dataIndex : 'report_name',
				text : Share.T('报告名称'),
				renderer : renderColor,
				listeners : {
                	dblclick : function(e) {
                		var model = Ext.getCmp('tab-report-report').getSelectionModel().getSelection()[0];
                		var report_hid = model.get('report_hid');
                		
                		Share.AjaxRequest({
							url : ctx + '/checkCondition',
							params : {
								report_hid : report_hid
							},
							callback : function(json) {
								if (json.resultcode) {
									var reportView = Ext.getCmp('tab-report-view-grid');
									if (reportView) {
										reportView.close();
									}
									var g = Ext.create("Ext.ButtonGroup");
						            g.style = 'background-color:#FFFFFF !important';
						            
						            var d = Ext.create('Ext.Button');
						            d.id = "report_export";
						            d.text = Share.T("导出");
						            d.scale = "large";
						            d.width = 82;
						            d.iconCls = "tj-head-export";
						            d.iconAlign = "top";
						            d.style = 'background-color:#FFFFFF !important';
						            g.add(d);
						            Ext.getCmp('actionbar').add(g);
			                		
									var node =
							        {    
							            "data" : { "id" : "report-view-grid", "text" : Share.T('报告查看'), "iconCls" : "tj-tree-file"}
							        };
									Share.opentTabByPanel(node, 'widget.reportcheck');
									
									//给报告查看页面中下拉框赋值，下拉框的作用就是存放当前报告的id，用来查询需要导出的数据
									Ext.getCmp('hidden_report_hid').setValue(report_hid);
								} else if(!json.resultcode) {
									Ext.Msg.alert('错误', Share.T('您所设置的过滤条件可能有误，请确认。'));
								} else{
									Share.ShowMessage('I0007', '');
								}
							}
						});
					}
				}
			}, {
				xtype : 'gridcolumn',
				width : 300,
				align : 'left',
				style:'text-align:center;',
				dataIndex : 'report_describe',
				text : Share.T('报告描述')
			}, {
				xtype : 'gridcolumn',
				width : 150,
				align : 'center',
				dataIndex : 'create_user',
				text : Share.T('创建人')
			}, {
				xtype : 'gridcolumn',
				width : 140,
				align : 'center',
				dataIndex : 'create_time',
				format : 'y/m/d H:i:s',
				text : Share.T('创建时间')
			} ],
			selModel: Ext.create('Ext.selection.CheckboxModel', {
				checkOnly: false
			}),
//			plugins :[{ptype:"rowselectionpaging"}],
			dockedItems : [{
                xtype : 'pagingtoolbar',
                id:'ReportUserDefinetoolbar',
                dock : 'bottom',
                width : 360,
                inputItemWidth : 45,
                displayInfo : true,
                store : 'Report',
                plugins : [{
                    ptype : "pagesize",
                    uxPageSize : page_size}]
            	}, {
				xtype : 'toolbar',
				dock : 'top',
				items : [ {
					xtype : 'tbspacer',
					width : 20
				}, {
					xtype : 'combo',
					id : 'reportall',
					editable : false,
					value : '0',
					valueField : 'value',
					displayField : 'text',
					store: new Ext.data.JsonStore({
						proxy: { 
							type: 'ajax', 
							url : ctx + '/getComboData',
        	                params:{},
        	                reader: {
        	                    type: 'json', 
        	                    totalProperty: '', 
        	                    root: '' 
        	                } 
						}, 
						fields:['text','value'], 
        	            autoLoad:true 
			        }),
					listeners : {
						beforerender:function(){
			        		this.store.removeAll();
			        		this.store.on('beforeload', function(store) {
			        			Ext.apply(store.proxy.extraParams , {
			        				type : 'definereport'
			        			});
			        		});
			        		this.store.load();
			        	},
						change : function(){
							var owner = Ext.getCmp('reportall').getValue();
							var store = Ext.getCmp('tab-report-report').getStore();
							store.on('beforeload', function(store) {
			         			Ext.apply(store.proxy.extraParams , {
			         				owner : owner,
			         				report_name : Ext.getCmp('reportname').getValue()
			         			});
							});
			                store.load();
						}
					}
				}, {
					xtype : 'tbspacer',
					width : 30
				}, {
					xtype : 'combo',
					id : 'reportname',
					fieldLabel : Share.T('报告名称'),
					emptyText : Share.T('查找报告...'),
					width : 200,
					labelWidth : 60,
					hideTrigger : true,
					valueField : 'value',
					displayField : 'text',
					store : new Ext.data.JsonStore({
    					proxy: { 
        	                type: 'ajax', 
        	                url : ctx + '/getReportNameForCom',
        	                params : {},
        	                reader : {
        	                    type : 'json',
        	                    totalProperty : '',
        	                    root : '' 
        	                } 
        	        },
        	        fields : ['text','value'], 
        	        autoLoad : true 
			        }),
					listeners : {
						expand : function() {
							this.store.load();
						},
    					beforequery : function(e) {
        					var combo = e.combo;   
        					if(!e.forceAll){   
        						var value = e.query;   
        						combo.store.filterBy(function(record,id){   
        							var text = record.get(combo.displayField); 
        							return (text.indexOf(value)!=-1);   
        						});
        						combo.expand();   
        						return false;   
        					}
    					},
    					select : function(){
    						var report_name = Ext.getCmp('reportname').getValue();
			        	    var store = Ext.getCmp('tab-report-report').getStore();
							   	store.on('beforeload', function(store) {
					        		Ext.apply(store.proxy.extraParams , {
					        			report_name : report_name,
					        			owner : Ext.getCmp('reportall').getValue()
					        		});
				               });
				            store.load();
    					}
    				}
				} ]
			} ]
		});
		me.callParent(arguments);
	}
});